<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="./css/bootstrap.min.css" />
    <link rel="stylesheet" href="./css/sign.css">
    <style>
        .logo {
            margin-right: 10px;
        }

        .nav-link {
            cursor: pointer;
        }

        .mainIfr {
            height: 1000px;
        }

        a {
            text-decoration: none;
        }
    </style>
</head>

<body>
    <div class="container">
        <header
            class="d-flex flex-wrap align-items-center justify-content-center justify-content-md-between py-3 mb-4 border-bottom">
            <a class="text-dark text-decoration-none" href="">
                <img class="logo" src="https://neustudy.neumooc.com/exam/favicon.ico" alt="">
                <span class="fs-4" id="title">个人网站</span>
            </a>
            <ul class="nav nav-pills">
                <li class="nav-item">
                    <div class="nav-link active" onclick="change(this)">首页</div>
                </li>
                <li class="nav-item">
                    <div class="nav-link" onclick="change(this)">作业1</div>
                </li>
                <li class="nav-item">
                    <div class="nav-link" onclick="change(this)">作业2</div>
                </li>
                <li class="nav-item">
                    <div class="nav-link" onclick="change(this)">作业3</div>
                </li>
                <li class="nav-item">
                    <div class="nav-link" onclick="change(this)">作业4</div>
                </li>
                <li class="nav-item">
                    <div class="nav-link" onclick="change(this)">用户列表</div>
                </li>
                <li class="nav-item">
                    <div class="nav-link" onclick="change(this)">登录日志</div>
                </li>
            </ul>
            <div class="col-md-3 text-end">
                <button class="btn btn-outline-primary me-2" onclick="showSignin()">
                    登录
                </button>
                <button class="btn btn-primary" onclick="showSignup()">
                    注册
                </button>
            </div>
        </header>
        <div class="mainIfr">
            <iframe id="myIframe" src="牛牛拼图/index.html" frameborder="0" width="100%" height="100%"></iframe>
        </div>

    </div>

    <div class="box" id="signin">
        <form action="">
            <h2>系统登录</h2>
            <span>Name</span>
            <input type="text" class="txt" id="userName">
            <span>Password</span>
            <input type="text" class="txt" id="password">
        </form>
        <button class="signin" onclick="login()">登录</button>
        <button class="esc" onclick="closeSignin()">取消</button>
    </div>

    <div class="box" id="signup">
        <form action="">
            <h2>系统注册</h2>
            <span>Name</span>
            <input type="text" class="txt" id="newUserName">
            <span>Password</span>
            <input type="text" class="txt" id="newPassword">
            <span>Phone</span>
            <input type="text" class="txt" id="phone">
            <span>E-mail</span>
            <input type="text" class="txt" id="email">
        </form>
        <button class="signup" onclick="register()">注册</button>
        <button class="esc" onclick="closeSignup()" type="submit">取消</button>
    </div>

    <script src="./js/jquery.min.js"></script>
    <script>
        function showSignin() {
            document.getElementById('signin').style.display = 'block'
        }

        function closeSignin() {
            document.getElementById('signin').style.display = 'none'
        }

        function showSignup() {
            document.getElementById('signup').style.display = 'block'
        }

        function closeSignup() {
            document.getElementById('signup').style.display = 'none'
        }

        function change(val) {
            let navArr = document.getElementsByClassName('nav-link')

            for (let i = 0; i < navArr.length; i++) navArr[i].classList.remove('active')

            val.className += ' active'

            let name = val.innerText

            let myIframe = document.getElementById('myIframe')

            if (name === '首页') {
                myIframe.src = '牛牛拼图/index.html'
            } else if (name === '作业1') {
                myIframe.src = './开飞机的小姐姐.html'
            } else if (name === '作业2') {
                myIframe.src = './如何阅读一本书.html'
            } else if (name === '作业3') {
                myIframe.src = './商品.html'
            } else if (name === '作业4') {
                myIframe.src = '贪吃蛇/index.html'
            } else if (name === '用户列表') {
                myIframe.src = './用户列表/index.html'
            } else if (name === '登录日志') {
                myIframe.src = './登录日志/index.html'
            }
        }
        function login() {

            let userName = document.getElementById('userName')
            let password = document.getElementById('password')

            let obj = {
                userName: userName.value,
                password: password.value
            }

            $.ajax({
                type: 'post',
                url: 'http://www.cloud-ygo.com.cn:33599/api/web-api/user/login',
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(obj),
                success: function(res) {
                    if(res.code !== 200) {
                        alert(res.msg)
                    } else {
                        alert('登录成功')
                        let str = document.getElementById('title').innerText
                        document.getElementById('title').innerText = res.body.userName + '的' + str
                        closeSignin();
                    }
                },
            })
        }

        function register() {
            let newName = document.getElementById('newUserName')
            let newPassword = document.getElementById('newPassword')
            let phone = document.getElementById('phone')
            let email = document.getElementById('email')

            let obj = {
                userName: newName.value,
                password: newPassword.value,
                phone: phone.value,
                email: email.value
            }

            $.ajax({
                type: 'post',
                url: 'http://www.cloud-ygo.com.cn:33599/api/web-api/user/register',
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(obj),
                success: function (res) {
                    if (res.code !== 200) {
                        alert(res.msg)
                    } else {
                        alert('注册成功')
                        closeSignup()
                    }
                },
            })
        }
    </script>
</body>

</html>